<template>
    <div>
        <Row>
            <i-col span="24">
                <Card>
                    <Button class="opt-btn" @click="showAddModal" type="primary">添加</Button>
                    <Dropdown class="opt-btn" @on-click="batchOpt">
                        <Button type="primary">导入导出
                        <Icon type="ios-arrow-down"></Icon>
                        </Button>
                        <DropdownMenu slot="list">
                        <DropdownItem name="import">
                            导入菜单
                        </DropdownItem>
                        <DropdownItem name="export">
                            <span style="color: green;">导出菜单</span>
                        </DropdownItem>
                        </DropdownMenu>
                    </Dropdown>
                    <Dropdown class="opt-btn" @on-click="batchOpt">
                        <Button type="primary">
                            批量操作
                            <Icon type="ios-arrow-down"></Icon>
                        </Button>
                        <DropdownMenu slot="list">
                            <DropdownItem name="batchActive">批量激活</DropdownItem>
                            <DropdownItem name="batchInactive"><span style="color: red;">批量冻结</span></DropdownItem>
                            <DropdownItem name="batchRemove" divided><span style="color: red;">批量删除</span></DropdownItem>
                        </DropdownMenu>
                    </Dropdown>
                    <Button class="opt-btn" @click="showSearchModal" type="primary">高级搜索</Button>
                    <Button class="opt-btn" @click="sonMenuOrder(0)" type="primary">一级菜单排序</Button>
                    <Tooltip class="opt-btn" content="刷新" placement="right">
                        <Button icon="md-refresh" type="success" shape="circle" @click="searchTable(false)"></Button>
                    </Tooltip>
                    <Tooltip class="opt-btn" content="重置搜索" placement="right">
                        <Button icon="md-repeat" type="success" shape="circle" @click="researchTable"></Button>
                    </Tooltip>
                    <SysMenuTableMain ref="table" v-on:searchTable="searchTable" v-on:showEditModal="showEditModal" v-on:showDetailModal="showDetailModal"
                      v-on:showMeta="showMeta"
                      v-on:addSonMenu="addSonMenu"
                      v-on:showSonMenu="showSonMenu"
                      v-on:sonMenuOrder="sonMenuOrder"
                    />
                </Card>
            </i-col>
        </Row>
        <SysMenuAddEditModal ref="addEditModal" v-on:add="add" v-on:edit="edit"/>
        <SysMenuSearchModal ref="searchModal" v-on:searchTable="searchTable"/>
        <SysMenuDetailModal ref="detailModal"/>
        <!-- 子菜单排序 -->
        <SysMenuOrderModal ref="menuOrderModal" />

        <!-- 查看菜单属性的模态窗 -->
        <Modal v-model="modal.menuMeta" title="菜单属性" :mask-closable="false" width="1160">
          <SysMenuMetaMain ref="menuMetaModal" />
          <div slot="footer">
            <Button type="text" size="large" @click="cancelModal('menuMeta')">关闭</Button>
          </div>
        </Modal>
        <!-- 查看子菜单的模态窗 -->
        <Modal v-model="modal.menu" title="子菜单" :mask-closable="false" width="1020">
          <SysMenuMainSingle ref="menuModal" />
          <div slot="footer">
            <Button type="text" size="large" @click="cancelModal('menu')">关闭</Button>
          </div>
        </Modal>
        <!-- 上传文件的模态窗 -->
        <UploadModal ref="uploadModal" @search="searchTable" :title="uploadModal.title" :format="uploadModal.format"/>
    </div>
</template>

<script>
    import * as utils from '@/api/utils-v2'
    import * as ResponseStatus from '@/api/response-status'
    import SysMenuTableMain from './SysMenuTableMain.vue'
    import SysMenuAddEditModal from './SysMenuAddEditModal.vue'
    import SysMenuSearchModal from './SysMenuSearchModal.vue'
    import SysMenuDetailModal from './SysMenuDetailModal.vue'
    import SysMenuMetaMain from '../sys-menu-meta/SysMenuMetaMain.vue'
    import SysMenuMainSingle from './SysMenuMainSingle.vue'
    import SysMenuOrderModal from './SysMenuOrderModal.vue'
    import UploadModal from '_c/upload-modal'
    export default {
        name: 'SysMenuMain',
        components: {
            SysMenuTableMain,
            SysMenuAddEditModal,
            SysMenuSearchModal,
            SysMenuDetailModal,
            SysMenuMetaMain,
            SysMenuMainSingle,
            SysMenuOrderModal,
            UploadModal
        },
        data() {
            return {
                urls: {
                    batchRemoveUrl: '/sys-menu/admin/batch-remove',
                    batchActiveUrl: '/sys-menu/admin/batch-active',
                    oneUrl: '/sys-menu/admin/one/',
                    exportUrl: '/permission-import-export/export-menu'
                },
                uploadModal: {
                    title: '导入菜单信息',
                    format: ['json'],
                    uploadUrl: '/permission-import-export/import-menu'
                },
                modal: {
                  menuMeta: false,
                  menu: false
                }
            }
        },
        computed: {},
        mounted() {},
        methods: {
            searchTable(reset) {
                utils.search(this, reset)
            },
            researchTable() {
                this.$refs.searchModal.resetForm()
                setTimeout(() => {
                    utils.search(this, true)
                }, 10);
            },
            showAddModal() {
                let addEditModal = this.$refs.addEditModal
                addEditModal.modal.add = true
            },
            add() {
                utils.add(this)
            },
            showEditModal(row) {
              let addEditModal = this.$refs.addEditModal
              if (row.parentId === 0) {
                row.parentName = '一级类目'
                addEditModal.modal.edit = true
                row.version = null
                row.createTime = null
                row.updateTime = null
                addEditModal.form = row
              } else {
                utils
                  .doGet(this.urls.oneUrl + row.parentId, {})
                  .then(res => {
                    if (ResponseStatus.OK === res.data.code) {
                      const name = res.data.data.name
                      if (name) {
                        row.parentName = name
                      }

                      addEditModal.modal.edit = true
                      row.version = null
                row.createTime = null
                row.updateTime = null
                addEditModal.form = row
                    } else {
                      this.$Message.error(res.data.message)
                    }
                  })
                  .catch(err => {
                    console.log(err)
                  })
              }
            },
            edit() {
                utils.edit(this)
            },
            showDetailModal(row) {
                let detailModal = this.$refs.detailModal
                detailModal.modal.detail = true
                detailModal.form = row
				detailModal.getDictItemsDisplayValue()
            },
            showSearchModal() {
                let searchModal = this.$refs.searchModal
                searchModal.modal.search = true
            },
            batchOpt(itemName) {
                if (itemName === 'batchActive') {
                    utils.batchActive(this, 0)
                } else if (itemName === 'batchInactive') {
                    utils.batchActive(this, 1)
                } else if (itemName === 'batchRemove') {
                    utils.batchRemove(this)
                } else if (itemName === 'import') {
                  // 导入菜单
                  this.showUploadModal()
                } else if (itemName === 'export') {
                  // 导出菜单
                  utils.exportJson(this)
                }
            },
            /**
             * 显示上传模态窗
             */
            showUploadModal() {
                this.$refs.uploadModal.uploadUrl = this.uploadModal.uploadUrl
                this.$refs.uploadModal.setUploadHeaders()
                this.$refs.uploadModal.uploadModal = true
            },
            showModal(modal) {
              utils.showModal(this, modal)
            },
            cancelModal(modal) {
              utils.cancelModal(this, modal)
            },
            /**
             * 查看菜单属性
             */
            showMeta(row) {
              let sysMenuMeta = this.$refs.menuMetaModal
              let searchModal = sysMenuMeta.$refs.searchModal
              searchModal.searchForm.id = row.id
              sysMenuMeta.menuId = row.id
              sysMenuMeta.searchTable()
              this.showModal('menuMeta')
            },
            /**
             * 新增子菜单
             */
            addSonMenu(row) {
              let addEditModal = this.$refs.addEditModal
              addEditModal.form.parentId = row.id
              addEditModal.form.parentName = row.name
              addEditModal.form.menuLevel = row.menuLevel + 1
              addEditModal.modal.add = true
            },
            /**
             * 查看子菜单
             */
            showSonMenu(row) {
              let menuModal = this.$refs.menuModal
              let searchModal = menuModal.$refs.searchModal
              searchModal.searchForm.parentId = row.id
              menuModal.searchTable()
              this.showModal('menu')
            },
            /**
             * 子菜单排序
             */
            sonMenuOrder(menuId) {
              this.$refs.menuOrderModal.menuOrderModal = true
              this.$refs.menuOrderModal.initData(menuId)
            }
        }
    }
</script>

<style>
</style>
